package com.bestv.search.common.dao.hibernate;

import com.bestv.search.common.dao.SearchKeywordDao;
import com.bestv.search.common.model.SearchKeyword;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author Michael Wang
 */
public class SearchKeywordDaoHibernate extends GenericDaoHibernate<SearchKeyword, Long> implements SearchKeywordDao {

    @Override
    public void saveSearchKeywordList(final List<SearchKeyword> keywordList) {
        getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                for (SearchKeyword keyword : keywordList) {
                    session.save(keyword);
                    session.flush();
                    session.clear();
                }
                return null;
            }
        });
    }

    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Integer> sumEachKeyword() {
        return (Map<String, Integer>) getHibernateTemplate().execute(new HibernateCallback() {
            public Map<String, Integer> doInHibernate(Session session) throws HibernateException, SQLException {
                Map<String, Integer> result = new HashMap<String, Integer>();
                String nativeSql = "select s.keyword as keyword,count(*) as count from search_keyword s group by s.keyword order by count desc";
                Query query = session.createSQLQuery(nativeSql).addScalar("keyword", Hibernate.STRING)
                        .addScalar("count", Hibernate.INTEGER);
                List<Object[]> list = query.list();
                for (Iterator<Object[]> iterator = list.iterator(); iterator.hasNext(); ) {
                    Object[] row = iterator.next();
                    result.put((String) row[0], (Integer) row[1]);
                }
                return result;
            }
        });
    }

    @Override
    public void removeAllKeywords() {
        getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                String hql = "delete SearchKeyword";
                Query query = session.createQuery(hql);
                return query.executeUpdate();
            }
        });
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<SearchKeyword> getSearchKeywords(String keyword) {
        return super.getHibernateTemplate().find("from SearchKeyword where keyword = ?" + keyword);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<SearchKeyword> getTimeSortedKeywords() {
        return super.getHibernateTemplate().find("from SearchKeyword order by hitTime");
    }
}
